<?php
class UserloginAction extends FrontAction {

	public $smarty = null;
	
	public function __construct(){
		parent::__construct();
	}
	
	/**
	 * 登录页面
	 * @return [type] [description]
	 */
	public function doIndex(){

		//$a = '056102d05bd3d9813215a4c850a29b714cf226f2a47a9f3efdf79868410603c946b9878098ba4f788eeadc862ec162313addb346c8ebddc461038ac19cfc52b9a50bc329c1d6eb1bcb1cf1ec23f67ddb5b5c25d5a4202d680289dce8ba1d1456aac53782349907eaa00866686e3c14dce6bf40f51871e7374ac629b070c61209';
		//$logininfo = CommonUtil::jsDeCrypt($a);

		//echo $logininfo;
		$usercache = new UserCache();
		$num = $usercache->setPasswordVerifyCodecache();

		$data = array();
		$data['vaildcodeshow'] = 0;
		if ($num > 5) {
			$data['vaildcodeshow'] = 1;
		}
		$this->smarty->assign("data", $data);
		$this->smarty->display('login_phone.html');
	}


    /**
     * 手机号登录
     * @access public
     * @return void
     */
    public function doLoginByPhone(){
        // 接收参数
        $params = Main::filter();
        if ($params['action'] == 'time') {
            $result = array(
                'status' => 200,
                'message' => "",
                'data' => array('time' => time())
            );
            echo json_encode($result);
            exit;
        }

        $result = array(
            'status' => 400,
            'message' => '登录失败',
            'url' => "?r=login"
        );

        //rsa 获取手机号和验证码
        $login_encrypt_data = $_POST['loginencrypt'];
        $logininfo = CommonUtil::jsDeCrypt($login_encrypt_data);
        $logininfo = explode("&", $logininfo);
        //5分钟有效
        if (count($logininfo) != 4 || time() - $logininfo[2] > 300) {
            $result['message'] = '登陆失败(01)';
            echo json_encode($result);
            exit;
        }

        $phone = $logininfo[0];
        $smscode = $logininfo[1];
        $userid = $logininfo[3];

//        $service = new SdkSmsService();
//        $res = $service->check($phone, $smscode);
//        if (!$res) {
//            $result['message'] = '验证码无效，请输入最新接收的验证码';
//            echo json_encode($result);
//            exit;
//        }

        $bmAdminModule = new BmAdminModule();
        $userinfo = $bmAdminModule->getUserInfoByPhone($phone);
        if(empty($userinfo)){
            $result['message'] = '该手机号未绑定账号，请联系管理员绑定';
            echo json_encode($result);
            exit;
        }
        if ($userinfo['isEnabled'] != 'Y'){
            $result['message'] = '账号信息异常，请联系管理员';
            echo json_encode($result);
            exit;
        }
        unset($userinfo['password']);

        //生成key，多平台切换验证
        $ptoken = Validator::token($phone);
        $userinfo["ptoken"] = $ptoken;
        $userinfo["login_phone"] = $phone;
        $userinfo['userid'] = $userinfo['adminid'];
        $userinfo['defaultrole'] = unserialize($userinfo['defaultrole']);

        Common::addLoginInfo($userinfo, LOGIN_SECURE_FRONT_KEY);
        $result = array(
            'status' => 200,
            'message' => '登录成功，跳转到首页！',
            'data' => array("url" => '?r=home')
        );

        exit(json_encode($result));
    }


	/**
	 * 登出
	 */
	public function doLoginout() {
		Common::clearLoginInfo(LOGIN_SECURE_FRONT_KEY);

		$data = array(
			"code" => 200,
			"message" => "已退出登录",
			"data" => array("url" => "?r=userlogin")
		);

		echo json_encode($data);
		exit;
	}

	/**
	 * 验证码
	 * @access public
	 * @return void | json
	 */
	public function doVerify(){
		$verifycode = new Verifycode();
	}

    /**
     * 发送短信验证码
     * @return void | json
     */
    public function doSendSMS() {
        $result = array(
            'status' => 400,
            'msg' => '发送失败',
        );

        $login_encrypt_data = $_POST['loginencrypt'];
        $logininfo = CommonUtil::jsDeCrypt($login_encrypt_data);
        $logininfo = explode("&", $logininfo);
        //5分钟有效
        if (count($logininfo) != 2 || time() - $logininfo[1] > 300) {
            echo json_encode($result);
            exit;
        }

        $phone = $logininfo[0];
        $service = new SdkSmsService();
        $res = $service->send($phone, 1);
        if (!$res) {
            $error = $service->getError();
            $result['msg'] = $error ? $error : '发送失败';
            echo json_encode($result);
            exit;
        }

        $result = array(
            'status' => 200,
            'msg' => '发送成功',
        );
        echo json_encode($result);
        exit;
    }
}


